<?php
include_once '../includes/common.php';
if($islogin2==1){}else sysmsg("您还未登录，请先登录",2,'./login.php',true);
if(isset($_GET['id'])) {
$id=isset($_GET['id'])?intval($_GET['id']):sysmsg("参数错误",2,'./',true);
$row=$DB->get_row("SELECT * FROM authguao_site WHERE id='{$id}' limit 1");
$program=$DB->get_row("SELECT * FROM authguao_program WHERE id='{$row['proid']}' limit 1");
if(!$row)sysmsg("授权平台不存在该授权",2,'./authlist.php',true);
if($row['uid']!=$userrow['uid'])sysmsg("不能编辑别人的下级",2,'./authlist.php',true);
if(!$program)sysmsg("授权平台不存在该程序",2,'./authlist.php',true);
$title='编辑授权';
include_once './header.php';
$prorow = $DB->get_row("select * from authguao_program where id='" . $row['proid'] . "' limit 1");
if ($prorow) {
    $pro_name = $prorow['name'];
} else {
    $pro_name = '未知程序';
}
?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                编辑授权
            </div>
            <div class="card-body">
                <form class="form-horizontal layui-form">
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">归属程序：</label>
                        <input type="text" name="name" value="<?php echo $pro_name?>" class="form-control" disabled/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">站点名称：</label>
                        <input type="text" name="name" value="<?php echo $row['name']; ?>" class="form-control" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">授权Q Q：</label>
                        <input type="number" name="qq" value="<?php echo $row['qq']; ?>" class="form-control" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">授权域名：</label>
                        <input type="text" name="url" value="<?php echo $row['url']; ?>" class="form-control" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <?php if($program['ipauth']==1){?>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">授权IP：</label>
                       <input type="text" name="ip" value="<?php echo $row['ip']; ?>" class="form-control" placeholder="留空则自动获取并记录"/>
                    </div>
                   <?php }?>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">授权码：</label>
                        <input type="text" name="authcode" value="<?php echo $row['authcode']; ?>" class="form-control" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">特征码：</label>
                        <input type="text" name="sign" value="<?php echo $row['sign']; ?>" class="form-control" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">是否激活：</label>
                        <select name="active" class="form-control" lay-search lay-filter="active"><option <?php echo $row['active'] == 1 ? 'selected ' : '' ?>value="1">1_是</option><option <?php echo $row['active'] == 0 ? 'selected ' : '' ?>value="0">0_否</option></select>
                    </div>
                    <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_siteedit">修 改</button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php
include_once './bottom.php';
?>
<script type="text/javascript">
layui.use(['form'], function () {
    var form = layui.form;
    form.on('submit(submit_siteedit)', function (data) {
        layer.alert('是否要执行当前操作？', {
            icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                siteedit();
            }
        });
        return false;
    });
});
function siteedit() {
    var name = $("input[name='name']").val();
    var qq = $("input[name='qq']").val();
    var url = $("input[name='url']").val();
    var ip = $("input[name='ip']").val();
    var authcode = $("input[name='authcode']").val();
    var sign = $("input[name='sign']").val();
    var active = $("select[name='active']").val();
    if (qq.length < 0 || qq.length > 200 || isNaN(qq)) {
        layer.msg('请输入授权QQ号', {icon: 5});
        return false;
    } else if (url == "") {
        layer.msg("请输入正确域名", {icon: 5});
        return false;
    } else if (authcode.length < 32) {
        layer.msg("授权代码错误", {icon: 5});
        return false;
    }
    var ii = layer.msg('正在编辑授权中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "POST",
        url: "ajax.php?act=siteedit&id=<?php echo $id;?>",
        data : {name:name,qq:qq,url:url,ip:ip,authcode:authcode,sign:sign,active:active},
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {icon: 6});
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
</script>
<?php
}else{
    sysmsg("参数错误",2,'./',true);
}
?>